#include "PLStringMethods.h"

using std::string;
using std::vector;

/**
  *	Tests, whether str ends with endString or not
  *
  * @param str string, that is used to find the endString at the end of it
  * @param endString string, which has to be found at the end of str to return true
  * @return true, when str ends with endString, false otherwise
  */
bool PL::StringEndsWith( const std::string &str, const std::string &endString )
{
	string::size_type length = endString.size();

	if (str.find( endString, str.size() - length ) == str.size() - length)
		return true;

	return false;
}

/**
  *	Tests, whether str starts with startString or not
  *
  * @param str string, that is used to find the startString at the beginning of it
  * @param startString string, which has to be found at the beginning of str to return true
  * @return true, when str starts with startString, false otherwise
  */
bool PL::StringStartsWith( const string &str, const string &startString )
{
	if (str.find( startString ) == 0)
		return true;

	return false;
}

/**
  *	Tokenizes the string in str using the characters in delimiters to add
  * all the found tokens to the vector tokens.
  *
  * @param[in] str const reference to the string that is tokenized
  * @param[out] pointer to the vector, that is used to add all the found tokens into
  * @param[in] delimiters const reference to a string, that contains all the characters that are used as delimiters
  */
void PL::Tokenize( const string &str, vector<string> *tokens,
	const string &delimiters )
{
	// Skip delimiters at beginning.
	string::size_type last_pos = str.find_first_not_of( delimiters, 0 );
	// Find first "non-delimiter".
	string::size_type pos = str.find_first_of( delimiters, last_pos );

	while (string::npos != pos || string::npos != last_pos)
	{
		// Found a token, add it to the vector.
		tokens->push_back( str.substr( last_pos, pos - last_pos ) );
		// Skip delimiters.  Note the "not_of"
		last_pos = str.find_first_not_of( delimiters, pos );
		// Find next "non-delimiter"
		pos = str.find_first_of( delimiters, last_pos );
	}
}